package com.zhongke.cotrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.ExpertInfoMapper;
import com.zhongke.cotrun.mapper.RegionMapper;
import com.zhongke.cotrun.mapper.ResearchMapper;
import com.zhongke.cotrun.mapper.UserInfoMapper;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.ExpertInfo;
import com.zhongke.cotrun.model.pojo.Region;
import com.zhongke.cotrun.model.pojo.UserInfo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.ExpertInfoService;
import com.zhongke.cotrun.service.RegionService;
import com.zhongke.cotrun.service.ResearchService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import com.zhongke.cotrun.shiro.web.entity.WebUserEntity;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.print.attribute.standard.Chromaticity;
import javax.servlet.http.HttpSession;
import java.beans.Transient;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@Service
public class ExpertInfoServiceImpl implements ExpertInfoService {
    @Autowired
    ExpertInfoMapper expertInfoMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    ResearchService researchService;
    @Resource
    RegionMapper regionMapper;

    /**
     * 修改审核状态
     *
     * @param expertInfo 参数
     *                   1 id
     *                   2 审核
     */

    @Override
    public boolean updateAuditStatus(ExpertInfo expertInfo) throws MyException {

        Short auditStatus = expertInfo.getAuditStatus();
        Integer userId = expertInfo.getLinkUserId();
        String identity = userInfoMapper.selectIdentity(userId);//身份
//1 先判断有没有认证过
        if (identity != null && !"".equals(identity)) {
            if (identity.contains("1")) {
                throw new MyException(CodeMsg.HASEXPERT_ERROR);
            }

            identity += "|1";
        }


        if (expertInfoMapper.updateAuditStatus(expertInfo)) {
            //1.代表通过
            if (auditStatus == 1) {

                //通过后给角色赋值审核状态
                if (userInfoMapper.setIdentityForuser(identity, userId)) {
                    return true;
                }
                return false;
            }


            return true;
        }


        return false;
    }

    /**
     * 按条件查询专家
     *
     * @param userInfo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo selectExpertInfoByCondition(UserInfo userInfo, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<ExpertInfo> o = expertInfoMapper.selectExpertByCondition(userInfo);

        if (o != null && o.size() != 0) {
            for (ExpertInfo e : o) {

                UserInfo u = e.getUserInfo();
                if(u==null){
                    continue;
                }
                String city = u.getCity();
                if (city == null || "".equals(city)) {
                    city = "0";
                }
                Region region = regionMapper.selectByPrimaryKey(Integer.valueOf(city));
                if (region!=null){
                    u.setCityName(region.getName());
                }



            }
        }
        PageInfo pageInfo = new PageInfo(o);
        return pageInfo;

    }

    /**
     * 查看具体的专家
     */
    @Override
    public ExpertInfo selectExpertById(ExpertInfo expertInfo) {

        ExpertInfo expertInfo1 = expertInfoMapper.selectExpertById(expertInfo);
////城市转换
//        UserInfo u = expertInfo1.getUserInfo();
//        String city = u.getCity();
//        if (city == null || "".equals(city)) {
//            city = "0";
//        }
//
//        u.setCity(String.valueOf(regionMapper.selectByPrimaryKey(Integer.valueOf(city))));

        //获取传递的值
        String code = expertInfo1.getResearchArea();


        //返给前端
        expertInfo1.setResearchArea(researchService.selectResearch(code, ","));
//        System.out.println(researchService.selectResearch(code, ","));
        return expertInfo1;
    }

    /**
     * 添加专家修改专家
     *
     * @param expertInfo
     * @return
     */
    @Override
    @Transient
    public Boolean addExperInfo(ExpertInfo expertInfo, HttpSession session) throws MyException {

//        UserInfo userInfo0 = ((UserInfo) session.getAttribute("USERINFO"));
        WebUserEntity userInfo0 = ShiroUtils.getWebUser();

        expertInfo.setLinkUserId(userInfo0.getUserId().intValue());//将用户的id设为当前的id

        if (userInfo0 == null) {
            throw new MyException(CodeMsg.USER_LOGIN_ERROR);
        }

        if (!"1".equals(userInfo0.getType())) {
            throw new MyException(CodeMsg.USER_TYPE_ERROR);
        }

        Integer userId = userInfo0.getUserId().intValue();//获取用户id
        //查询专家发没发布过
        ExpertInfo expertInfo1 = expertInfoMapper.selectExpertById(expertInfo);
        UserInfo userInfo = expertInfo.getUserInfo();
        userInfo.setUserId(userId);


//       修改用户信息暂时不要
//        if (userInfoMapper.updateUserInfo(userInfo)) {
//               }
//        return false;

        if (expertInfo1 != null) {
            Integer expertId = expertInfo1.getExpertId();
            expertInfo.setExpertId(expertId);

            return expertInfoMapper.updateExpertInfo(expertInfo);
        }

        return expertInfoMapper.insertExperInfo(expertInfo);


    }

    /**
     * 查看自己的信息
     * @param userId
     * @return
     */
    @Override
    public ExpertInfo selectMyExpertInfo(Integer userId) {
        return expertInfoMapper.selectMyExpertByuUserId(userId);
    }
    /**
     * 根据id查看专家信息
     * @param
     * @return
     */
    @Override
    public ExpertInfo selectMyExpertByuId(Integer id) {
        return expertInfoMapper.selectMyExpertByuId(id);
    }

}
